import Service from '@/api/httpAxios'

export interface FleetsParamType {
  project_id: string
  offset: number
  limit: number
}

export interface FleetType {
  project_id: string
  fleet: CreateFleetRequestBody
}

export interface UpdateFleetRequest {
  name?: string
  description?: string
  server_session_protection_policy?: string
  server_session_protection_time_limit_minutes?: number
  enable_auto_scaling?: boolean
  instance_tags: Array<instanceTags>
}

export interface UpdateFleetInstanceCapacityRequest {
  min_instance_number?: number
  max_instance_number?: number
  desired?: number
}

export interface UpdateFleetInboundPermissionsRequest {
  inbound_permission_authorizations: Array<IpPermission>
  inbound_permission_revocations: Array<IpPermission>
}

export interface UpdateFleetRuntimeConfigurationRequest {
  server_session_activation_timeout_seconds?: number
  max_concurrent_server_sessions_per_process?: number
  process_configurations?: Array<ProcessConfiguration>
}
export interface FleetBasicMsg {
  project_id: string
  fleet_id: string
  UpdateFleetRequestBody: UpdateFleetRequest
}

export interface FleetRulesMsg {
  project_id: string
  fleet_id: string
  UpdateFleetInboundPermissionsRequestBody: UpdateFleetInboundPermissionsRequest
}

export interface FleetConfigMsg {
  project_id: string
  fleet_id: string
  UpdateFleetRuntimeConfigurationRequestBody: UpdateFleetRuntimeConfigurationRequest
}

export interface FleetCapacityMsg {
  project_id: string
  fleet_id: string
  UpdateFleetInstanceCapacityRequestBody: UpdateFleetInstanceCapacityRequest
}
export interface CreateFleetRequestBody {
  name: string
  description?: string
  build_id: string
  region?: string
  instance_type?: string
  bandwidth?: number | string
  public_ip: publicIp
  server_session_protection_policy?: string
  server_session_protection_time_limit_minutes?: number | string
  enterprise_project_id?: string
  vpc_id?: string
  subnet_id?: string
  runtime_configuration: RuntimeConfiguration
  instance_tags: Array<instanceTags>
  security_group_ids?: Array<string>
  available_zones?: Array<string>
  scaling_group_capacity: scalingGroupCapacity
  resource_creation_limit_policy?: ResourceCreationLimitPolicy
  inbound_permissions: Array<IpPermission>
  resource_specification_id?: string
}
interface publicIp {
  ip_type?: string
  bandwidth: bandwidth
}
interface bandwidth {
  share_type?: string
  size?: number | string
  charging_mode?: string
  id?: string
}
export interface scalingGroupCapacity {
  min_instance_number?: number | string
  max_instance_number?: number | string
  desire_instance_number?: number | string
}
export interface ResourceCreationLimitPolicy {
  policy_period_in_minutes?: number
  new_sessions_per_creator?: number
}

export interface RuntimeConfiguration {
  server_session_activation_timeout_seconds?: number
  max_concurrent_server_sessions_per_process?: number
  process_configurations: Array<ProcessConfiguration>
}

export interface ProcessConfiguration {
  launch_path: string
  parameters: string
  concurrent_executions: number
}

export interface IpPermission {
  protocol: string
  ip_range: string
  from_port: number
  to_port: number
}
export interface instanceTags {
  key: string
  value: string
  [key: string]: string | boolean
}
class FleetsService {
  getFleets = (obj: FleetsParamType) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/fleets`
    return Service.get<any>(url, {
      params: {
        offset: obj.offset,
        limit: obj.limit
      }
    })
  }

  createFleet = (obj: FleetType) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/fleets`
    return Service.post<any>(url, obj.fleet)
  }

  updateBasicMsg = (obj: FleetBasicMsg) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/fleets/${obj.fleet_id}`
    return Service.put<any>(url, obj.UpdateFleetRequestBody)
  }

  updateRules = (obj: FleetRulesMsg) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/fleets/${obj.fleet_id}/inbound-permissions`
    return Service.put<any>(url, obj.UpdateFleetInboundPermissionsRequestBody)
  }

  updateConfig = (obj: FleetConfigMsg) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/fleets/${obj.fleet_id}/runtime-configuration`
    return Service.put<any>(url, obj.UpdateFleetRuntimeConfigurationRequestBody)
  }

  updateCapacity = (obj: FleetCapacityMsg) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/fleets/${obj.fleet_id}/instance-capacity`
    return Service.put<any>(url, obj.UpdateFleetInstanceCapacityRequestBody)
  }
}

export default new FleetsService()
